$.extend({
	doPostGetLocationList:function(pid, obj, url) {		
		$.post(url, 
			{parentID: pid}, 
			function(xml) {
				var str = $.getXMLStringOf(xml, 'location');
				$(obj).html(str);
			}
		);
	},
	
	doPostGetCategoryList:function(pid, obj, url) {		
		$.post(url, 
			{parentID: pid}, 
			function(xml) {
				var str = $.getXMLStringOf(xml, 'category');		
				$(obj).html(str);
			}
		);
	},
	
	getXMLObject:function(xml) {
		try { //Internet Explorer
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(xml);
		} catch(e){ //Firefox et. all
			try {
				parser = new DOMParser();
				xmlDoc = parser.parseFromString(xml, "text/xml");
				xmlDoc.async = "false";
			} catch (e) {
				alert(e.message);
				return null;
			}
		} finally {
			return xmlDoc;
		}
	},
	
	getSingleNode:function(name, xmlDoc) {
		xmlDoc = $.getXMLObject(xmlDoc);
		var obj = xmlDoc.getElementsByTagName(name)[0];
		return obj.text;
	},
	
	getStringFromXMLQuestions:function(xml) {
		xmlDoc = $.getXMLObject(xml);
		if (xmlDoc != null) {			
			var questionList = xmlDoc.getElementsByTagName('Question');
			if (questionList.length > 0) {
				var str = '';
				for ( var i = 0; i < questionList.length; i++) {
					var question = questionList[i];
					str += '<b>' + (i + 1) + '. ' + question.getAttribute('content') + '</b><br />';
					var answerList = question.getElementsByTagName('answer');
					for (var j = 0; j < answerList.length; j++) {
						var answer = answerList[j];
						str += '<input type="radio" value="' + question.getAttribute('id') + '-' 
							+ answer.getAttribute('id') 
							+ '" name="' + question.getAttribute('id') + '" />'
							+ answer.getAttribute('content') + '<br />'; 
					}
					str += '<br />';
				}
			}
			return str;
		}
	},
	
	getStringFromXMLProviders:function(xml) {
		var str = '<h1>Danh sách địa điểm</h1>';
		xmlDoc = $.getXMLObject(xml);
		if (xmlDoc != null) {			
			var providerList = xmlDoc.getElementsByTagName('Provider');
			if (providerList.length > 0) {				
				str += '<fieldset class="form"><ol>';
				provider = providerList[0];
				var url = "index.php?view&type=provider&id=" + provider.getAttribute('id');
				str += '<li class="input"><input type="radio" name="diadiem" checked="checked" value="' 
					+ provider.getAttribute('id') + '" />'
					+ '<a href="' + url + '">' + provider.getAttribute('name') + ' - ' 
					+ provider.getAttribute('address') + '</a>'
					+ '</li>';
				for ( var i = 1; i < providerList.length; i++) {
					provider = providerList[i];
					url = "index.php?view&type=provider&id=" + provider.getAttribute('id');
					str += '<li class="input"><input type="radio" name="diadiem" value="' 
						+ provider.getAttribute('id') + '" />'
						+ '<a href="' + url + '">' + provider.getAttribute('name') + ' - ' 
						+ provider.getAttribute('address') + '</a>'
						+ '</li>';
				}
				str += '</ol></fieldset>';
			}
			return str;
		}
	},
	
	//get ket qua danh gia item tu dialog box
	getDanhGiaItem:function() {
		str = '';
		$('#contentDanhGiaItem input[type=radio]:checked').each(				
			function(index){				
				str += $(this).val() + ' ';				
			}
		);
		return str;
	},
	
	//get ket qua danh gia item tu dialog box
	getDanhGiaProvider:function() {
		str = '';
		$('#contentDanhGiaProvider input[type=radio]:checked').each(				
			function(index){				
				str += $(this).val() + ' ';				
			}
		);
		return str;
	},
	
	//chuyen danh sach cac povider
	/*getXMLStringOf:function(xml, name) {		
		xmlDoc = $.getXMLObject(xml);
		if (xmlDoc != null) {
			var locationList = xmlDoc.getElementsByTagName(name);
			var str = '';
			if (locationList.length > 0) {
				var location = locationList[0];
				str += '<option value="' + location.getAttribute("id") + '" selected="selected">' + location.getAttribute("name") + '</option>';
			}
			for ( i = 1; i < locationList.length; i++) {
				var location = locationList[i];
				var id = location.getAttribute("id");
				var name = location.getAttribute("name");									
				str += '<option value="' + id + '">' + name + '</option>';																													
			}				
				
			return str;
		} else {
			return '';
		}
	},*/
	
	removeRow:function(tag) {		
		$(tag).parent().parent().remove();
	},
	
	emailValidate:function(strEmail) {
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;			

		if (strEmail.search(validRegExp) == -1) {
			return false;
		}
		return true;
	},
	
	isInteger:function(s){
		var i;
	    for (i = 0; i < s.length; i++){   
	        // Check that current character is number.
	        var c = s.charAt(i);
	        if (((c < "0") || (c > "9"))) return false;
	    }
	    // All characters are numbers.
	    return true;
	},

	stripCharsInBag:function(s, bag){
		var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
	    for (i = 0; i < s.length; i++){   
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) returnString += c;
	    }
	    return returnString;
	},

	daysInFebruary:function(year){
		// February has 29 days in any year evenly divisible by four,
	    // EXCEPT for centurial years which are not also divisible by 400.
	    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	},
	
	getDaysArray:function(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
			if (i==2) {this[i] = 29}
	   } 
	   return this;
	},
	
	isDate:function(dtStr) {
		// Declaring valid date character, minimum year and maximum year
		var dtCh= "/";
		var minYear=1900;
		var maxYear=2100;
		
		var daysInMonth = $.getDaysArray();
		var pos1 = dtStr.indexOf(dtCh);
		var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
		var strDay = dtStr.substring(0, pos1);
		var strMonth = dtStr.substring(pos1 + 1, pos2);
		var strYear = dtStr.substring(pos2 + 1);
		
		strYr = strYear;
		if (strDay.charAt(0)=="0" && strDay.length>1) 
			strDay=strDay.substring(1);
		if (strMonth.charAt(0)=="0" && strMonth.length>1) 
			strMonth=strMonth.substring(1);
		for (var i = 1; i <= 3; i++) {
			if (strYr.charAt(0)=="0" && strYr.length>1) { 
				strYr=strYr.substring(1);
			}
		}
		month = parseInt(strMonth);
		day = parseInt(strDay)
		year = parseInt(strYr)
		if (pos1 == -1 || pos2 == -1) {
			return false;				
		}
		if (strMonth.length < 1 || month < 1 || month > 12) {
			return false;
		}
		if (strDay.length < 1 || day < 1 || day > 31 || ( month == 2 && day > $.daysInFebruary(year)) || day > daysInMonth[month]){
			return false;
		}
		if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
			return false;
		}
		if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || $.isInteger($.stripCharsInBag(dtStr, dtCh))==false) {
			return false;
		}
		return true;	
	},
	favouriteProvider:function(id,name,url) {		
		$.post(url, 
				{id: id}, 
				function(data) {
					if (data != '') {
						alert('Bạn đã yêu thích địa điểm ' + name);
					} else {
						alert('Có lỗi xảy ra khi yêu thích địa điểm ' + name + '. Bạn hãy thử lại !!!');
					}
				}
		);
	},
	favouriteItem:function(id,name,url) {		
		$.post(url, 
				{id: id}, 
				function(data) {
					if (data != '') {
						alert('Bạn đã yêu thích địa điểm ' + name);
					} else {
						alert('Có lỗi xảy ra khi yêu thích item ' + name + '. Bạn hãy thử lại !!!');
					}
				}
		);
	},
	trim:function(s) {
		return $.rtrim($.ltrim(s));		
	},
	ltrim:function(s) {
		var l=0;
		while(l < s.length && s[l] == ' ') {	
			l++; 
		}
		return s.substring(l, s.length);
	},
	rtrim:function(s) {
		var r=s.length -1;
		while(r > 0 && s[r] == ' ')
		{	r-=1;	}
		return s.substring(0, r+1);
	}
});			
